package com.cu.employmentmanagementsystem.handler;

import cn.dev33.satoken.stp.StpUtil;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author 25853
 */
@Component
public class MetaObjectHandler implements com.baomidou.mybatisplus.core.handlers.MetaObjectHandler
{
    @Override
    public void insertFill(MetaObject metaObject)
    {
        //判断是否有这个属性
        boolean hasCreateTime = metaObject.hasSetter("createdTime");
        boolean hasUpdateTime = metaObject.hasSetter("updateTime");
        boolean uploadTime = metaObject.hasSetter("uploadTime");
        boolean createAccount = metaObject.hasSetter("createdAccount");
        boolean updateAccount = metaObject.hasSetter("updateAccount");
        boolean uploadAccount = metaObject.hasSetter("uploadAccount");

        //如果有这个属性就设置值
        if (hasCreateTime)
        {
            setFieldValByName("createdTime", LocalDateTime.now(), metaObject);
        }
        if (hasUpdateTime)
        {
            setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
        }
        if (uploadTime)
        {
            setFieldValByName("uploadTime", LocalDateTime.now(), metaObject);
        }
        if (createAccount)
        {
            setFieldValByName("createdAccount", StpUtil.getLoginIdAsString(), metaObject);
        }
        if (updateAccount)
        {
            setFieldValByName("updateAccount", StpUtil.getLoginIdAsString(), metaObject);
        }
        if (uploadAccount)
        {
            setFieldValByName("uploadAccount", StpUtil.getLoginIdAsString(), metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject)
    {
        boolean hasUpdateTime = metaObject.hasSetter("updateTime");
        boolean updateAccount = metaObject.hasSetter("updateAccount");


        if (hasUpdateTime)
        {
            setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
        }
        if (updateAccount)
        {
            setFieldValByName("updateAccount", StpUtil.getLoginIdAsString(), metaObject);
        }
    }
}
